package com.yidu.hbats.action.atsData;



import com.yidu.hbats.model.AtsBaseItem;
import com.yidu.hbats.model.AtsBaseSport;
import com.yidu.hbats.model.AtsBaseSportItem;
import com.yidu.hbats.service.atsBase.AtsBaseSportItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.hbats.service.impl.atsData.AtsDataBatchImpl;
import com.yidu.hbats.service.target.AtsBaseItemService;
import com.yidu.utils.AtsBaseItemTree;
import com.yidu.utils.Message;
import com.yidu.utils.TreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @auto:ltx
 * @date:2019-11-01-14:33:21
 * 控制层
 */
public class AtsBaseSportController {
    //批次service
    private AtsDataBatchImpl atsBaseSportService;
    //指标service
    private AtsBaseItemService atsBaseItemService;
    //关联表service
    private AtsBaseSportItemService atsBaseSportItemService;

    /**
     * 查询所有分页
     * @param sportName 项目名称
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public Map<String,Object> selectAll(Integer page,Integer limit,String sportName){
        Map<String,Object> map = new HashMap<String,Object>();
        //System.out.println(sportName);
        //文本框值不为空
        if(sportName!= null){
            map.put("data",atsBaseSportService.selectAll(1,limit,sportName));
            map.put("count",atsBaseSportService.count(sportName));
            map.put("code",0); // 放入 code ，0
        }else{
            map.put("data",atsBaseSportService.selectAll(page,limit,null));
            map.put("count",atsBaseSportService.count(null));
            map.put("code",0); // 放入 code ，0
        }

        return map;
    }

    /**
     * 删除
     * @param sportId
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public Message delete(Integer sportId){
        //设置删除
        Integer count =  atsBaseSportService.deleteUtis(sportId);
        Message message = new Message();
        if(count > 0 ){
            message.setNum(1);
            message.setMsg("删除成功");
        }else{
            message.setNum(1);
            message.setMsg("删除失败");
        }
        return  message;
    }

    /**
     *  项目指标下拉树
     * @return 返回个集合
     */
    @RequestMapping("/treeNode")
    @ResponseBody
    public  List<TreeNode> findTreeNode(Integer id){
        //查询所有的指标
        List<AtsBaseItemTree> list = atsBaseItemService.selectAll();
        //System.out.println(id+"===================");
        //查询到该项目所有指标
        List<AtsBaseSportItem> atsBaseSportItems = atsBaseSportItemService.selectByPrimaryKey2(id);
        String ids = "";
        for (Iterator<AtsBaseSportItem> iterator = atsBaseSportItems.iterator(); iterator.hasNext(); ) {
            AtsBaseSportItem next =  iterator.next();
            //将该项目所有指标装入
            ids += next.getItemId().toString()+",";
        }
        //分割成数组
        String[] ida = ids.split(",");
        //修改数据格式返回
        return tree(list,ida);
    }
    /**
     * 计划指标下拉树
     * @return 返回个集合
     */
    @RequestMapping("/jihuaNode")
    @ResponseBody
    public  List<TreeNode> findItemNode(Integer id){
        //获取所有指标
        List<AtsBaseItemTree> list = atsBaseItemService.selectAll();
        //System.out.println(id+"===================");
        //查询到该项目所有指标
        List<AtsBaseSportItem> atsBaseSportItems = atsBaseSportItemService.selectByPrimaryKey2(id);
        String ids = "";
        for (Iterator<AtsBaseSportItem> iterator = atsBaseSportItems.iterator(); iterator.hasNext(); ) {
            AtsBaseSportItem next =  iterator.next();
            //将该项目所有指标装入
            ids += next.getItemId().toString()+",";
        }
        //根据,分割成数组
        String[] ida = ids.split(",");
        //修改数据格式返回
        return tree(list,ida);
    }
    /**
     * 新增
     * @param session 登录用户session
     * @param atsBaseSport 数据对象
     * @param itemIds 指标id
     * @return
     */
    @ResponseBody
    @RequestMapping("insert")
    public Message insert(HttpSession session, AtsBaseSport atsBaseSport, String itemIds, String jihuaIds){
        //System.out.println(us.getManUser()+"用户名");
        //设置创建时间
        atsBaseSport.setCreatTime(new Date());
        //新增
        int count = atsBaseSportService.insert(atsBaseSport,itemIds,jihuaIds);
        //返回数据格式
        Message message= new Message();
        if (count>0){
            message.setMsg("新增成功");
            message.setNum(1);
        }else{
            message.setMsg("新增失败");
            message.setNum(0);
        }
        return message;
    }

    /**
     * 修改
     * @param atsBaseSport
     * @param itemIds
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public Message update(HttpSession session,AtsBaseSport atsBaseSport, String itemIds,String jihuaIds){
        //设置创建时间
        atsBaseSport.setCreatTime(new Date());
        int count = 0;
        //创建集合1
        List<AtsBaseSportItem> atsBaseSportItems1 = new ArrayList<>();
        //创建集合3
        List<AtsBaseSportItem> atsBaseSportItems3 = new ArrayList<>();
        //将前台传入指标ID分割成数组
        String itemId[]  = itemIds.split(",");
        for (int i = 0; i < itemId.length; i++) {

            AtsBaseSportItem atsBaseSportItem = new AtsBaseSportItem();
            //设置创建时间
            atsBaseSportItem.setCreatTime(new Date());
            //设置项目ID
            atsBaseSportItem.setSportId(atsBaseSport.getSportId());
            //设置状态默认为1
            atsBaseSportItem.setIsva(1);
            //备注
            atsBaseSportItem.setRemark(atsBaseSport.getRemark());
            //指标ID
            atsBaseSportItem.setItemId(Integer.parseInt(itemId[i]));
            //创建人
            atsBaseSportItem.setCreater(atsBaseSport.getCreater());
            //新增
            atsBaseSportItems1.add(atsBaseSportItem);
        }
        //计划ID数组
        String jihuaId[]  = jihuaIds.split(",");
        for (int i = 0; i < jihuaId.length; i++) {
            //创建关联表对象
            AtsBaseSportItem atsBaseSportItem = new AtsBaseSportItem();
            //设置创建时间
            atsBaseSportItem.setCreatTime(new Date());
            //设置项目ID
            atsBaseSportItem.setSportId(atsBaseSport.getSportId());
            //设置状态默认为1
            atsBaseSportItem.setIsva(2);
            //备注
            atsBaseSportItem.setRemark(atsBaseSport.getRemark());
            //指标ID
            atsBaseSportItem.setItemId(Integer.parseInt(itemId[i]));
            //创建人
            atsBaseSportItem.setCreater(atsBaseSport.getCreater());
            //新增
            atsBaseSportItems3.add(atsBaseSportItem);
        }
        //调用修改方法
        count = atsBaseSportService.updateByPrimaryKey(atsBaseSport,atsBaseSportItems1,atsBaseSportItems3);


        //返回数据
        Message message = new Message();
        if (count > 0) {
            message.setMsg("修改成功");
            message.setNum(1);
        } else {
            message.setMsg("修改失败");
            message.setNum(0);
        }
        return message;
    }

    /**
     * 查询所有
     * @return 数据
     */
    @ResponseBody
    @RequestMapping("findAll")
    public List<AtsBaseSport> findAll(){
        return atsBaseSportService.findAll();
    }

    /**
     * 更改数据格式
     * @param a 原数据
     * @param at 该项目指标数组
     * @return 新数据
     */
    public  List<TreeNode> tree(List<AtsBaseItemTree> a, String[] at){
        List<TreeNode> list = new ArrayList<>();
        if(a.size() > 0 ) {
            for (AtsBaseItemTree ats : a) {
                TreeNode tre = new TreeNode();
                //过滤Id为0的指标
                if(!ats.getId().equals("0")){
                    tre.setId(ats.getValue());
                    tre.setTitle(ats.getTitle());
                    tre.setValue(ats.getId());

                    for (int i = 0; i < at.length; i++) {
                        if (at.length >0) {
                            //查询该项目拥有指标并选中
                            if (at[i].equals(tre.getValue())) {

                                tre.setChecked(true);
                            }
                        }
                        //如果子指标存在则继续循环
                        if (ats.getChildren().size() > 0 && ats.getChildren() != null) {
                            tre.setData(tree(ats.getChildren(), at));
                        }
                    }
                    list.add(tre);
                }
            }
            return list;
        }else{
            return list;
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public AtsBaseSport findById(Integer id){
        return atsBaseSportService.selectByPrimaryKey(id);
    }

    /**
     * 修改指标
     * @param atsBaseSport 体育项目
     * @return 修改结果
     */
    @ResponseBody
    @RequestMapping("updateItem")
    public Message updateItem(AtsBaseSport atsBaseSport,String itemIds){
        //切割成数组
        String[]ids = itemIds.split(",");
        int count=0;
        for (int i = 0; i < ids.length; i++) {
            AtsBaseSportItem atsBaseSportItem = new AtsBaseSportItem();
            //设置创建时间
            atsBaseSportItem.setCreatTime(new Date());
            //设置项目ID
            atsBaseSportItem.setSportId(atsBaseSport.getSportId());
            //设置状态默认为1
            atsBaseSportItem.setIsva(1);
            //备注
            atsBaseSportItem.setRemark(atsBaseSport.getRemark());
            //指标ID
            atsBaseSportItem.setItemId(Integer.parseInt(ids[i]));
            //创建人
            atsBaseSportItem.setCreater(atsBaseSport.getCreater());
            //新增
            count =atsBaseSportItemService.insertSelective(atsBaseSportItem);
        }


        //返回数据
        Message message = new Message();
        if (count > 0) {
            message.setMsg("修改成功");
            message.setNum(1);
        } else {
            message.setMsg("修改失败");
            message.setNum(0);
        }
        return message;
    }
}
